package com.azxcrm.project.monitor.workflow.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.framework.web.controller.BaseController;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.framework.web.page.TableDataInfo;
import com.azxcrm.project.monitor.workflow.domain.WorkFlow;
import com.azxcrm.project.monitor.workflow.service.WorkFlowService;

/**
 * 工作流 控制层处理
 * 
 * @author Dustyone
 * @date 2018-08-07
 */
@Controller
@RequestMapping("/module/workFlow")
public class WorkFlowController extends BaseController
{
    private String prefix = "module/workFlow";
	
	@Autowired
	private WorkFlowService workFlowService;
	
	@GetMapping()
	@RequiresPermissions("module:workFlow:view")
	String workFlow()
	{
	    return prefix + "/workFlow";
	}
	
	/**
	 * 查询工作流列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("module:workFlow:list")
	@ResponseBody
	public TableDataInfo list(WorkFlow workFlow)
	{
		setPageInfo(workFlow);
        List<WorkFlow> list = workFlowService.selectWorkFlowStatesList(workFlow);
		return getDataTable(list);
	}
	
	/**
	 * 新增工作流
	 */
	@GetMapping("/add")
	@RequiresPermissions("module:workFlow:add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改工作流
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("module:workFlow:edit")
	public String edit(@PathVariable("id") Integer id, Model model)
	{
		WorkFlow workFlow = workFlowService.selectWorkFlowById(id);
		model.addAttribute("workFlow", workFlow);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存工作流
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("module:workFlow:add")
	public Message save(WorkFlow workFlow)
	{
		if (workFlowService.saveWorkFlow(workFlow) > 0)
		{
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 删除工作流
	 */
	@PostMapping( "/remove/{id}")
	@ResponseBody
	@RequiresPermissions("module:workFlow:remove")
	public Message remove(@PathVariable("id") Integer id)
	{	
		
		if(workFlowService.checkFlowIsApplyed(id)){
			//如果该工作流被应用过不予以被删除
			return Message.error(1, "该流程已被应用,不允许删除");
		}else{
			if (workFlowService.deleteWorkFlowById(id) > 0)
			{
			    return Message.ok();
			}
		}
		return Message.error();
	}
	
	/**
	 * 批量删除工作流
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("module:workFlow:batchRemove")
	public Message remove(@RequestParam("ids[]") Integer[] ids)
	{	
		for(int i = 0;i<ids.length;i++){
			if(workFlowService.checkFlowIsApplyed(ids[i])){
				WorkFlow worFlow = workFlowService.selectWorkFlowById(ids[i]);
				//如果该工作流被应用过不予以被删除
				return Message.error(1, "该流程 ["+worFlow.getFlowName()+"] 已被应用,不允许删除");
			}
		}
		
		int rows = workFlowService.batchDeleteWorkFlow(ids);
		if (rows > 0)
        {
            return Message.ok();
        }
        return Message.error();
	}
	
	
	
}
